﻿using FB.Data.Housekeep.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FB.Data.Housekeep
{
    public class HSKRepository<TEntity> : Repository<HousekeepDbContext, TEntity> where TEntity : class
    {
        public HSKRepository(HousekeepDbContext context) : base(context) { }
    }

    public class HousekeepUnit : UnitOfWorkBase<HousekeepDbContext>
    {
        Hashtable _repositories;

        public HSKRepository<T> Repository<T>() where T : class
        {
            if (_repositories == null)
                _repositories = new Hashtable();

            var type = typeof(T).Name;

            if (!_repositories.ContainsKey(type))
            {
                var repositoryType = typeof(HSKRepository<>);

                var repositoryInstance = Activator.CreateInstance(repositoryType.MakeGenericType(typeof(T)), _context);
                _repositories.Add(type, repositoryInstance);
            }

            return (HSKRepository<T>)_repositories[type];
        }

        public HousekeepUnit(HousekeepDbContext context) : base(context) { }
    }
}
